package com.example.roadship;

import java.util.ArrayList;
import net.simonvt.menudrawer.MenuDrawer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import class_sources.QuickActionItem;
import class_sources.RealPopupForQuickAction;
import class_sources.XMLProc;

//@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
	
	private Button btnTrip,btnOptions,btnQuick;
	private static final int ID_Trip = 1;
	private static final int  ID_Criterion = 2,ID_CLOSE=3;
	public static String xmlFilePath=Environment.getExternalStorageDirectory().toString() +"/RoadShip.xml";
	public static String session,command,commandMessage="";
	public static String nameSpace="urn:ProductsService",url="http://www.codediary.net/service/roadship_service.php";
	public ArrayList<String> names = new ArrayList<String>(), sessions = new ArrayList<String>();
    /*Slider Menu Tools*/
    private static final int MENU_OVERFLOW = 1;
	private XMLProc xmlHelper=new XMLProc();
	/*Login Values*/
	public static String userName = "";
	public static String password = "";
	/*Handler Objects*/
	public static UserMainHandler userHandler;
	public static SliderMenuHandler sliderHandler;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
    	
		/*SET USER SESSION*/
		try {
			session=xmlHelper.ReadXML(xmlFilePath).get("session").toString();
			userName=xmlHelper.ReadXML(xmlFilePath).get("username").toString();
			password=xmlHelper.ReadXML(xmlFilePath).get("password").toString();
			Log.i("USER ACCOUNT", userName+"----"+password);
					
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		/*Get User Friend List*/
		if(!session.equals(""))
		{
			userHandler = new UserMainHandler(userName, password, session ,getApplicationContext());
		}
		sliderHandler = new SliderMenuHandler(getApplicationContext() ,getLayoutInflater(), this ,userHandler);
		
	     /*Main Menu*/
		 ImageView image = (ImageView) findViewById(R.id.backgroungImage);
		 btnTrip=(Button)findViewById(R.id.btnMainBlue);
		 btnOptions=(Button)findViewById(R.id.btnMainPink);
		 btnQuick=(Button)findViewById(R.id.btnQuick);
		 QuickActionItem tripItem    = new QuickActionItem(ID_Trip, "Yolculuk", getResources().getDrawable(R.drawable.ic_travel));
	     QuickActionItem criterionItem     = new QuickActionItem(ID_Criterion, "Kriterler", getResources().getDrawable(R.drawable.ic_criterion));
	     QuickActionItem closeItem     = new QuickActionItem(ID_CLOSE, "Kapat", getResources().getDrawable(R.drawable.ic_sclose));
	     final RealPopupForQuickAction quickActionPopUp = new RealPopupForQuickAction(this, RealPopupForQuickAction.HORIZONTAL);
	     quickActionPopUp.addActionItem(tripItem);
	     quickActionPopUp.addActionItem(criterionItem);
	     quickActionPopUp.addActionItem(closeItem);
	     quickActionPopUp.setOnActionItemClickListener(new RealPopupForQuickAction.OnActionItemClickListener() {           
	        @Override
	        public void onItemClick(RealPopupForQuickAction source, int pos, int actionId) {               
	            //filtering items by id
	            if (actionId == ID_Trip)
	            {
	            		Intent i=new Intent(MainActivity.this,ChooseRotation.class);	
	            		startActivity(i);
	            }
	            else if (actionId == ID_Criterion) {
	            	    Intent i = new Intent(MainActivity.this, UserPropsActivity.class);
	            	    i.putExtra("session", session);
                	    startActivity(i);
	            }
	            else if (actionId == ID_CLOSE) {
                    quickActionPopUp.dismiss();
                }
	        }
	    });
		
		 btnQuick.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
					
				Intent i=new Intent(MainActivity.this,GoogleMapActivity.class);
				startActivity(i);
			}
		 });
		 btnOptions.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent i=new Intent(MainActivity.this,OptionsActivity.class);
					startActivity(i);
				}
		 });
		 btnTrip.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {					
					quickActionPopUp.show(v);					
					
				}
				
		 });
		 //End Of Main Menu Buttons
    }
	 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_sign_up, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_OVERFLOW:
            	sliderHandler.mDrawerRight.toggleMenu();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onBackPressed() {
        final int drawerState1 = sliderHandler.mDrawerRight.getDrawerState();
        final int drawerState2 = sliderHandler.mDrawerLeft.getDrawerState();
        if (drawerState1 == MenuDrawer.STATE_OPEN || drawerState1 == MenuDrawer.STATE_OPENING) {
        	sliderHandler.mDrawerRight.closeMenu();
            return;
        }
        if (drawerState2 == MenuDrawer.STATE_OPEN || drawerState2 == MenuDrawer.STATE_OPENING) {
        	sliderHandler.mDrawerLeft.closeMenu();
            return;
        }
        super.onBackPressed();
    }
}
